package com.example.a410.mybt;

import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import me.caibou.rockerview.JoystickView;

import static com.example.a410.mybt.MainActivity.mMainActivity;


public class RemoteControlActivity extends AppCompatActivity {

    private JoystickView joystickView;
    private Button mAngleBtn;
    private Button mSendMassage;

    // 123456789
    private Button mNum1Btn;
    private Button mNum2Btn;
    private Button mNum3Btn;
    private Button mNum4Btn;
    private Button mNum5Btn;
    private Button mNum6Btn;
    private Button mNum7Btn;
    private Button mNum8Btn;
    private Button mNum9Btn;

    private boolean sendZFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remote_control);

        joystickView = (JoystickView)findViewById(R.id.direct_control);
        mAngleBtn = (Button)findViewById(R.id.angle);
        mSendMassage = (Button) findViewById(R.id.send_massage);
        sendZFlag = false;

        mNum1Btn = (Button)findViewById(R.id.num1_button);
        mNum2Btn = (Button)findViewById(R.id.num2_button);
        mNum3Btn = (Button)findViewById(R.id.num3_button);
        mNum4Btn = (Button)findViewById(R.id.num4_button);
        mNum5Btn = (Button)findViewById(R.id.num5_button);
        mNum6Btn = (Button)findViewById(R.id.num6_button);
        mNum7Btn = (Button)findViewById(R.id.num7_button);
        mNum8Btn = (Button)findViewById(R.id.num8_button);
        mNum9Btn = (Button)findViewById(R.id.num9_button);

        // 取消显示顶部标题栏
        ActionBar actionbar = getSupportActionBar();
        if (actionbar != null) {
            actionbar.hide();
        }

        if (!mMainActivity.getBlueToothIsConnented()) {
            promptDialog();
        }

        AlertDialog.Builder dialog = new AlertDialog.Builder(RemoteControlActivity.this);
        dialog.setTitle("提示");
        dialog.setMessage("上/下/左/右分别发送 w/s/a/d，点击按钮发送相应字符。");
        dialog.setCancelable(false);
        dialog.setPositiveButton("我知道了", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        dialog.show();

        joystickView.setAngleUpdateListener(new JoystickView.OnAngleUpdateListener() {
            @Override
            public void onAngleUpdate(double angle, int action) {
                if (action == JoystickView.ACTION_RELEASE) {
                    if (sendZFlag) {
                        mMainActivity.sendData("z");
                        mSendMassage.setText("z");
                        sendZFlag = false;
                    }
                    mSendMassage.setText(" ");
                    mAngleBtn.setText("中心位置 " + angle);
                } else {
                    sendZFlag = true;
                    if (angle < 45 || angle > 315) {
                        mAngleBtn.setText("右 ：" + angle);
                        mMainActivity.sendData("d");
                        mSendMassage.setText("d");
                    } else if (angle >= 45 && angle < 135) {
                        mAngleBtn.setText("下：" + angle);
                        mMainActivity.sendData("s");
                        mSendMassage.setText("s");
                    } else if (angle >= 135 && angle < 225) {
                        mAngleBtn.setText("左： " + angle);
                        mMainActivity.sendData("a");
                        mSendMassage.setText("a");
                    } else {
                        mAngleBtn.setText("上：" + angle);
                        mMainActivity.sendData("w");
                        mSendMassage.setText("w");
                    }
                }
            }
        });



        mNum1Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        mMainActivity.sendData("1");
                        mSendMassage.setText("1");
                        return true; // if you want to handle the touch event
                    }
                    case MotionEvent.ACTION_UP: {
                        mMainActivity.sendData("z");
                        mSendMassage.setText("z");
                        return true; // if you want to handle the touch event
                    }
                }
                return false;
            }
        });

        mNum2Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        mMainActivity.sendData("2");
                        mSendMassage.setText("2");
                        return true; // if you want to handle the touch event
                    }
                    case MotionEvent.ACTION_UP: {
                        mMainActivity.sendData("z");
                        mSendMassage.setText("z");
                        return true; // if you want to handle the touch event
                    }
                }
                return false;
            }
        });

        mNum3Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        mMainActivity.sendData("3");
                        mSendMassage.setText("3");
                        return true; // if you want to handle the touch event
                    }
                    case MotionEvent.ACTION_UP: {
                        mMainActivity.sendData("z");
                        mSendMassage.setText("z");
                        return true; // if you want to handle the touch event
                    }
                }
                return false;
            }
        });

        mNum4Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        mMainActivity.sendData("4");
                        mSendMassage.setText("4");
                        return true; // if you want to handle the touch event
                    }
                    case MotionEvent.ACTION_UP: {
                        mMainActivity.sendData("z");
                        mSendMassage.setText("z");
                        return true; // if you want to handle the touch event
                    }
                }
                return false;
            }
        });

        mNum5Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        mMainActivity.sendData("5");
                        mSendMassage.setText("5");
                        return true; // if you want to handle the touch event
                    }
                    case MotionEvent.ACTION_UP: {
                        mMainActivity.sendData("z");
                        mSendMassage.setText("z");
                        return true; // if you want to handle the touch event
                    }
                }
                return false;
            }
        });

        mNum6Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        mMainActivity.sendData("6");
                        mSendMassage.setText("6");
                        return true; // if you want to handle the touch event
                    }
                    case MotionEvent.ACTION_UP: {
                        mMainActivity.sendData("z");
                        mSendMassage.setText("z");
                        return true; // if you want to handle the touch event
                    }
                }
                return false;
            }
        });

        mNum7Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        mMainActivity.sendData("7");
                        mSendMassage.setText("7");
                        return true; // if you want to handle the touch event
                    }
                    case MotionEvent.ACTION_UP: {
                        mMainActivity.sendData("z");
                        mSendMassage.setText("z");
                        return true; // if you want to handle the touch event
                    }
                }
                return false;
            }
        });

        mNum8Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        mMainActivity.sendData("8");
                        mSendMassage.setText("8");
                        return true; // if you want to handle the touch event
                    }
                    case MotionEvent.ACTION_UP: {
                        mMainActivity.sendData("z");
                        mSendMassage.setText("z");
                        return true; // if you want to handle the touch event
                    }
                }
                return false;
            }
        });

        mNum9Btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        mMainActivity.sendData("9");
                        mSendMassage.setText("9");
                        return true; // if you want to handle the touch event
                    }
                    case MotionEvent.ACTION_UP: {
                        mMainActivity.sendData("z");
                        mSendMassage.setText("z");
                        return true; // if you want to handle the touch event
                    }
                }
                return false;
            }
        });
    }

    void promptDialog() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(RemoteControlActivity.this);
        dialog.setTitle("提示");
        dialog.setMessage("设备未成功连接！");
        dialog.setCancelable(false);
        dialog.setPositiveButton("重新连接", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        dialog.show();
    }
}
